home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / REGUTIL.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1999-07-21  |  13.7 KB  |  330 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import win32api
  5. import win32con
  6. import string
  7. import sys
  8. import os
  9. error = 'Registry utility error'
  10. CLSIDPyFile = '{b51df050-06ae-11cf-ad3b-524153480001}'
  11. RegistryIDPyFile = 'Python.File'
  12. RegistryIDPycFile = 'Python.CompiledFile'
  13.  
  14. def GetRootKey():
  15.     '''Retrieves the Registry root in use by Python.
  16. \t'''
  17.     return win32con.HKEY_LOCAL_MACHINE
  18.  
  19.  
  20. def GetRegistryDefaultValue(subkey, rootkey = None):
  21.     '''A helper to return the default value for a key in the registry.
  22.         '''
  23.     if rootkey is None:
  24.         rootkey = GetRootKey()
  25.     
  26.     return win32api.RegQueryValue(rootkey, subkey)
  27.  
  28.  
  29. def SetRegistryDefaultValue(subKey, value, rootkey = None):
  30.     '''A helper to set the default value for a key in the registry
  31.         '''
  32.     import types
  33.     if rootkey is None:
  34.         rootkey = GetRootKey()
  35.     
  36.     if type(value) == types.StringType:
  37.         typeId = win32con.REG_SZ
  38.     elif type(value) == types.IntType:
  39.         typeId = win32con.REG_DWORD
  40.     else:
  41.         raise TypeError, 'Value must be string or integer - was passed ' + str(value)
  42.     win32api.RegSetValue(rootkey, subKey, typeId, value)
  43.  
  44.  
  45. def BuildDefaultPythonKey():
  46.     '''Builds a string containing the path to the current registry key.
  47.  
  48. \t   The Python registry key contains the Python version.  This function
  49. \t   uses the version of the DLL used by the current process to get the
  50. \t   registry key currently in use.
  51.         '''
  52.     return 'Software\\Python\\PythonCore\\' + sys.winver
  53.  
  54.  
  55. def GetAppPathsKey():
  56.     return 'Software\\Microsoft\\Windows\\CurrentVersion\\App Paths'
  57.  
  58.  
  59. def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None):
  60.     """Register a .exe file that uses Python.
  61.  
  62. \t   Registers the .exe with the OS.  This allows the specified .exe to
  63. \t   be run from the command-line or start button without using the full path,
  64. \t   and also to setup application specific path (ie, os.environ['PATH']).
  65.  
  66. \t   Currently the exeAppPath is not supported, so this function is general
  67. \t   purpose, and not specific to Python at all.  Later, exeAppPath may provide
  68. \t   a reasonable default that is used.
  69.  
  70. \t   exeFullPath -- The full path to the .exe
  71. \t   exeAlias = None -- An alias for the exe - if none, the base portion
  72. \t             of the filename is used.
  73. \t   exeAppPath -- Not supported.
  74. \t"""
  75.     if exeAppPath:
  76.         raise error, 'Do not support exeAppPath argument currently'
  77.     
  78.     if exeAlias is None:
  79.         exeAlias = os.path.basename(exeFullPath)
  80.     
  81.     win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + '\\' + exeAlias, win32con.REG_SZ, exeFullPath)
  82.  
  83.  
  84. def GetRegisteredExe(exeAlias):
  85.     '''Get a registered .exe
  86. \t'''
  87.     return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + '\\' + exeAlias)
  88.  
  89.  
  90. def UnregisterPythonExe(exeAlias):
  91.     '''Unregister a .exe file that uses Python.
  92. \t'''
  93.     
  94.     try:
  95.         win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + '\\' + exeAlias)
  96.     except win32api.error:
  97.         (code, fn, details) = None
  98.         import winerror
  99.         if code != winerror.ERROR_FILE_NOT_FOUND:
  100.             raise win32api.error, (code, fn, desc)
  101.         
  102.         return None
  103.  
  104.  
  105.  
  106. def RegisterNamedPath(name, path):
  107.     '''Register a named path - ie, a named PythonPath entry.
  108. \t'''
  109.     keyStr = BuildDefaultPythonKey() + '\\PythonPath'
  110.     if name:
  111.         keyStr = keyStr + '\\' + name
  112.     
  113.     win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
  114.  
  115.  
  116. def UnregisterNamedPath(name):
  117.     '''Unregister a named path - ie, a named PythonPath entry.
  118. \t'''
  119.     keyStr = BuildDefaultPythonKey() + '\\PythonPath\\' + name
  120.     
  121.     try:
  122.         win32api.RegDeleteKey(GetRootKey(), keyStr)
  123.     except win32api.error:
  124.         (code, fn, details) = None
  125.         import winerror
  126.         if code != winerror.ERROR_FILE_NOT_FOUND:
  127.             raise win32api.error, (code, fn, desc)
  128.         
  129.         return None
  130.  
  131.  
  132.  
  133. def GetRegisteredNamedPath(name):
  134.     '''Get a registered named path, or None if it doesnt exist.
  135. \t'''
  136.     keyStr = BuildDefaultPythonKey() + '\\PythonPath'
  137.     if name:
  138.         keyStr = keyStr + '\\' + name
  139.     
  140.     
  141.     try:
  142.         return win32api.RegQueryValue(GetRootKey(), keyStr)
  143.     except win32api.error:
  144.         (code, fn, details) = None
  145.         import winerror
  146.         if code != winerror.ERROR_FILE_NOT_FOUND:
  147.             raise win32api.error, (code, fn, desc)
  148.         
  149.         return None
  150.  
  151.  
  152.  
  153. def RegisterModule(modName, modPath):
  154.     '''Register an explicit module in the registry.  This forces the Python import
  155.            mechanism to locate this module directly, without a sys.path search.  Thus
  156.            a registered module need not appear in sys.path at all.
  157.  
  158. \t   modName -- The name of the module, as used by import.
  159. \t   modPath -- The full path and file name of the module.
  160. \t'''
  161.     
  162.     try:
  163.         import os
  164.         os.stat(modPath)
  165.     except os.error:
  166.         print 'Warning: Registering non-existant module %s' % modPath
  167.  
  168.     win32api.RegSetValue(GetRootKey(), BuildDefaultPythonKey() + '\\Modules\\%s' % modName, win32con.REG_SZ, modPath)
  169.  
  170.  
  171. def UnregisterModule(modName):
  172.     '''Unregister an explicit module in the registry.
  173.  
  174. \t   modName -- The name of the module, as used by import.
  175. \t'''
  176.     
  177.     try:
  178.         win32api.RegDeleteKey(GetRootKey(), BuildDefaultPythonKey() + '\\Modules\\%s' % modName)
  179.     except win32api.error:
  180.         (code, fn, desc) = None
  181.         import winerror
  182.         if code != winerror.ERROR_FILE_NOT_FOUND:
  183.             raise win32api.error, (code, fn, desc)
  184.         
  185.     except:
  186.         code != winerror.ERROR_FILE_NOT_FOUND
  187.  
  188.  
  189.  
  190. def GetRegisteredHelpFile(helpDesc):
  191.     '''Given a description, return the registered entry.
  192. \t'''
  193.     
  194.     try:
  195.         return GetRegistryDefaultValue(BuildDefaultPythonKey() + '\\Help\\' + helpDesc)
  196.     except win32api.error:
  197.         return None
  198.  
  199.  
  200.  
  201. def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1):
  202.     '''Register a help file in the registry.
  203. \t
  204. \t     Note that this used to support writing to the Windows Help
  205. \t     key, however this is no longer done, as it seems to be incompatible.
  206.  
  207.            helpFile -- the base name of the help file.
  208.            helpPath -- the path to the help file
  209.            helpDesc -- A description for the help file.  If None, the helpFile param is used.
  210.            bCheckFile -- A flag indicating if the file existence should be checked.
  211. \t'''
  212.     if helpDesc is None:
  213.         helpDesc = helpFile
  214.     
  215.     fullHelpFile = os.path.join(helpPath, helpFile)
  216.     
  217.     try:
  218.         if bCheckFile:
  219.             os.stat(fullHelpFile)
  220.     except os.error:
  221.         raise ValueError, 'Help file does not exist'
  222.  
  223.     win32api.RegSetValue(GetRootKey(), BuildDefaultPythonKey() + '\\Help\\%s' % helpDesc, win32con.REG_SZ, fullHelpFile)
  224.  
  225.  
  226. def UnregisterHelpFile(helpFile, helpDesc = None):
  227.     '''Unregister a help file in the registry.
  228.  
  229.            helpFile -- the base name of the help file.
  230.            helpDesc -- A description for the help file.  If None, the helpFile param is used.
  231. \t'''
  232.     key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\Help', 0, win32con.KEY_ALL_ACCESS)
  233.     
  234.     try:
  235.         
  236.         try:
  237.             win32api.RegDeleteValue(key, helpFile)
  238.         except win32api.error:
  239.             (code, fn, desc) = None
  240.             import winerror
  241.             if code != winerror.ERROR_FILE_NOT_FOUND:
  242.                 raise win32api.error, (code, fn, desc)
  243.             
  244.         except:
  245.             code != winerror.ERROR_FILE_NOT_FOUND
  246.  
  247.     finally:
  248.         win32api.RegCloseKey(key)
  249.  
  250.     if helpDesc is None:
  251.         helpDesc = helpFile
  252.     
  253.     
  254.     try:
  255.         win32api.RegDeleteKey(GetRootKey(), BuildDefaultPythonKey() + '\\Help\\%s' % helpDesc)
  256.     except win32api.error:
  257.         (code, fn, desc) = None
  258.         import winerror
  259.         if code != winerror.ERROR_FILE_NOT_FOUND:
  260.             raise win32api.error, (code, fn, desc)
  261.         
  262.     except:
  263.         code != winerror.ERROR_FILE_NOT_FOUND
  264.  
  265.  
  266.  
  267. def RegisterCoreDLL(coredllName = None):
  268.     '''Registers the core DLL in the registry.
  269.  
  270.         If no params are passed, the name of the Python DLL used in 
  271.         the current process is used and registered.
  272. \t'''
  273.     if coredllName is None:
  274.         coredllName = win32api.GetModuleFileName(sys.dllhandle)
  275.     else:
  276.         
  277.         try:
  278.             os.stat(coredllName)
  279.         except os.error:
  280.             print 'Warning: Registering non-existant core DLL %s' % coredllName
  281.  
  282.     hKey = win32api.RegCreateKey(GetRootKey(), BuildDefaultPythonKey())
  283.     
  284.     try:
  285.         win32api.RegSetValue(hKey, 'Dll', win32con.REG_SZ, coredllName)
  286.     finally:
  287.         win32api.RegCloseKey(hKey)
  288.  
  289.     win32api.RegSetValue(GetRootKey(), 'Software\\Python\\PythonCore\\CurrentVersion', win32con.REG_SZ, sys.winver)
  290.  
  291.  
  292. def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
  293.     """Register the core Python file extensions.
  294. \t
  295. \t   defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
  296. \t   defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
  297. \t   runCommand -- The command line to use for running .py files
  298. \t"""
  299.     pythonFileId = RegistryIDPyFile
  300.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, '.py', win32con.REG_SZ, pythonFileId)
  301.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, pythonFileId, win32con.REG_SZ, 'Python File')
  302.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, '%s\\CLSID' % pythonFileId, win32con.REG_SZ, CLSIDPyFile)
  303.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, '%s\\DefaultIcon' % pythonFileId, win32con.REG_SZ, defPyIcon)
  304.     base = '%s\\Shell' % RegistryIDPyFile
  305.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\Open', win32con.REG_SZ, 'Run')
  306.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\Open\\Command', win32con.REG_SZ, runCommand)
  307.     pythonFileId = RegistryIDPycFile
  308.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, '.pyc', win32con.REG_SZ, pythonFileId)
  309.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, pythonFileId, win32con.REG_SZ, 'Compiled Python File')
  310.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, '%s\\DefaultIcon' % pythonFileId, win32con.REG_SZ, defPycIcon)
  311.     base = '%s\\Shell' % pythonFileId
  312.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\Open', win32con.REG_SZ, 'Run')
  313.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\Open\\Command', win32con.REG_SZ, runCommand)
  314.  
  315.  
  316. def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None):
  317.     base = '%s\\Shell' % RegistryIDPyFile
  318.     if shellUserCommand:
  319.         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\%s' % shellCommand, win32con.REG_SZ, shellUserCommand)
  320.     
  321.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\%s\\Command' % shellCommand, win32con.REG_SZ, exeCommand)
  322.  
  323.  
  324. def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
  325.     base = '%s\\Shell' % RegistryIDPyFile
  326.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\%s\\ddeexec' % shellCommand, win32con.REG_SZ, ddeCommand)
  327.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\%s\\ddeexec\\Application' % shellCommand, win32con.REG_SZ, ddeApp)
  328.     win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT, base + '\\%s\\ddeexec\\Topic' % shellCommand, win32con.REG_SZ, ddeTopic)
  329.  
  330.